import { _decorator, Component, director, instantiate, Node, UITransform, v3, Vec2 } from 'cc';
import { Monster } from './Monster';
import { PLayer } from './Player';
import { Fire } from '../skill/Fire';
const { ccclass, property } = _decorator;

export class FightService {


    private static i = 0
    private constructor() { }

    private static allMonster: Monster[] = [];

    public static addMonster(monster: Monster) {
        this.allMonster.push(monster);
    }

    public static get canAttackMonster(): Monster {
        return this.allMonster[0];
    }

    static castSkill(player: PLayer) {
        const monster = this.canAttackMonster;
        if (!monster) {
            return;
        }

        const playerPos = player.node.getPosition();
        const monsterPos = monster.node.getPosition();
        // console.log(`玩家位置${playerPos}`+','+`怪物位置${monsterPos}`);


        const playerUITf = player.node.getComponent(UITransform);
        const pos = playerUITf.convertToWorldSpaceAR(v3(playerPos.x, playerPos.y, 0));


        //计算玩家朝向怪物的方向向量
        if (this.i < 100000) {
            this.i++


            const dir = monsterPos.subtract(playerPos).normalize();
          //  console.log(dir.x+","+dir.y);

            const firePrefabNode = instantiate(player.firePrefab);
            const fireScript = firePrefabNode.getComponent(Fire)
            fireScript.init(playerPos, new Vec2(dir.x,dir.y));


            const parentNode =  player.node.parent;
            firePrefabNode.parent = parentNode
           //console.log(`玩家位置${playerPos}` + ',' + `怪物位置${monsterPos}`);
        }

    }

}


